/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUICliente.java
 *
 * Created on 20/11/2011, 23:19:14
 */

package GUI;

import java.util.Vector;

import tp1POO.Console;
import tp1POO.Verifica;
import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Persistencia.Pesquisa;
import tp1POO.Modelo.*;
import GUI.GUIErro;
import javax.swing.JOptionPane;
import tp1POO.GestorLog;
/**
 *
 * @author matheus
 */
public class GUICliente extends javax.swing.JFrame {
    private String msg="";
    /** Creates new form GUICliente */
    public GUICliente() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPCliente = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoCpf = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoEnd = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoTel = new javax.swing.JTextField();
        botCancel = new javax.swing.JButton();
        botOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastrar Cliente");

        jPCliente.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados do Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N

        jLabel1.setText("Nome:");

        jLabel2.setText("CPF:");

        jLabel3.setText("Endereço:");

        jLabel4.setText("Telefone:");

        javax.swing.GroupLayout jPClienteLayout = new javax.swing.GroupLayout(jPCliente);
        jPCliente.setLayout(jPClienteLayout);
        jPClienteLayout.setHorizontalGroup(
            jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campoNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                    .addComponent(campoEnd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPClienteLayout.createSequentialGroup()
                        .addComponent(campoTel, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPClienteLayout.setVerticalGroup(
            jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        botCancel.setText("Fechar");
        botCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botCancelActionPerformed(evt);
            }
        });

        botOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/certo.gif"))); // NOI18N
        botOk.setText("Ok");
        botOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPCliente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botCancel)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botCancel, botOk});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botCancel)
                    .addComponent(botOk))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botCancel, botOk});

        pack();
    }// </editor-fold>//GEN-END:initComponents







    private boolean erroCliente () {
        boolean erro=false;
        this.msg = "";

        erro = !Verifica.nomeCliente(campoNome.getText()).isEmpty() || !Verifica.cpfCliente(campoCpf.getText()).isEmpty()||campoTel.getText().isEmpty()||campoEnd.getText().isEmpty();
        if (campoTel.getText().isEmpty()) {
            msg = msg + "ERRO : Telefone Inválido!\n";
        }
        if (campoEnd.getText().isEmpty()) {
            msg = msg + "ERRO : Endereço Inválido!\n";
        }
        msg = msg + Verifica.nomeCliente(campoNome.getText()) + Verifica.cpfCliente(campoCpf.getText());
        if (erro) {
            JOptionPane.showMessageDialog(jPCliente,msg, "Error", JOptionPane.ERROR_MESSAGE);
        }

        return erro;
    }











    private void botOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botOkActionPerformed
                Cliente cliente = null;
		String nome=null, cpf=null;

		if(!erroCliente()) {
			nome = campoNome.getText();
			cpf = campoCpf.getText();
			cliente = new Cliente(nome, cpf);
			cliente.setEndereco(campoEnd.getText());
			cliente.setTelefone(campoTel.getText());
                        GerenciadorObjetos.obterInstancia().salvarObj(cliente);
                        campoNome.setText("");
                        campoTel.setText("");
                        campoCpf.setText("");
                        campoEnd.setText("");
                        GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+" - Cliente cadastrado com sucesso!\n\tNome : "+
                                cliente.getNome()+"\n\tCPF :"+cliente.getCpf()+"\n\tEndereço : "+
                                cliente.getEndereco()+"\n\tTelefone : "+cliente.getTelefone());
                        JOptionPane.showMessageDialog(jPCliente,"Cliente cadastrado com Sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
		}

		
    }//GEN-LAST:event_botOkActionPerformed

    private void botCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botCancelActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Adicionar clientes foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiCliente();
    }//GEN-LAST:event_botCancelActionPerformed


















    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUICliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botCancel;
    private javax.swing.JButton botOk;
    private javax.swing.JTextField campoCpf;
    private javax.swing.JTextField campoEnd;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoTel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPCliente;
    // End of variables declaration//GEN-END:variables

}
